var express = require('express');
var url = require('url');
var querystring = require('querystring');
var router = express.Router();
var users = require('../models').User;
var pwdHash = require('password-hash');
var utils = require('../utils');

/* GET users listing. */
router.get('/', function(req, res) {
    var user = req.session.user;
    if(user) {
        res.redirect('user/' + user.id.toString() + '/detail')
    } else {
        res.redirect('user/login');
    }
});

/*
User Login
 */
router.get('/login',function (req, res, next) {
	res.render('user/login',{title: '登录'});
}).post('/login', function (req, res, next) {
    var form = {
        email: req.body.email,
        password: req.body.password
    };
    users.findByEmail(form.email).then(function (user) {
        if(!user){
            res.render('message', {message:'User not exits!'});
            //res.redirect('/');
        }
        if(!pwdHash.verify(form.password, user.password)){
            res.render('message', {message:"Password not correct!"});
            //res.redirect('/login');
        }else {
            utils.LoginSession(req, res, user);
            res.redirect('/');
        }
    })
});

/*
User Register
 */
router.get('/register',function (req, res, next) {
	res.render('user/register', {title: '注册'});
}).post('/register',function (req, res, next) {
    var form = {
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
        telephoneNumber: req.body.telephone,
        ID_TYPE: req.body.id_type,
        ID_Number: req.body.id_number
    };
    users.findByEmail(form.email).then(function (user) {
        if(user) {
            res.render('message', {message:"User exits!"});
        }
        else {
            if(form.password.toString() == req.body.confirm_password.toString()) {
                form.password = pwdHash.generate(form.password);
                users.create(form).then(function (user) {
                    if(user){
                        utils.LoginSession(req, res, user);
                        res.redirect('/');
                    }
                    else {
                        res.render('message', {message:"Register failed!"});
                    }
                });
            }
            else {
                res.render('message', {message:"please confirm your password!"});
            }
        }
    });
});

/*
* log out*/
router.get('/logout', function (req, res, next) {
    utils.logoutSession(req);
    return res.redirect('/');
});
/*
* User detail
* */
router.get('/'+'([0-9]+)'+'/detail', function (req, res, next) {
    users.findById(req.session.user.id).then(function (User) {
        res.render('user/detail', {
            title: req.session.user.name.toString() + ': 用户信息',
            user: req.session.user,
            User_info: User
        });
    });
});

/*
* Change Password*/
router.get('/([0-9]+)/changepwd', function (req, res, next) {
    users.findById(req.session.user.id).then(function () {
        res.render('user/changepwd', {
            title: req.session.user.name.toString() + ': 修改密码',
            user: req.session.user
        });
    });
}).post('/([0-9]+)/changepwd', function (req, res, next) {
    var form = {
        user_id: req.session.user.id,
        old_pwd: req.body.old_password,
        new_pwd: req.body.new_password,
        confirm_pwd: req.body.confirm_password
    };
    users.findById(form.user_id).then(function (user) {
        if (!pwdHash.verify(form.old_pwd, user.password)) {
            res.render('message', {message:'password incorrect!'});
        } else {
            if (form.new_pwd == form.confirm_pwd) {
                var password = pwdHash.generate(form.new_pwd);
                users.updatePwd(form.user_id, form.new_pwd).then(function (new_pwd) {
                    if (new_pwd) {
                        res.render('message', {message:'change successfully！'});
                        //res.redirect('user/' + user.id.toString() + '/detail');
                    }
                    else {
                        res.render('message', {message:'change failed'});
                    }
                })
            }
            else {
                res.render('message', {message:'please confirm password!'});
            }
        }
    })
});


module.exports = router;
